﻿using System;
using System.Windows;
using System.Windows.Input;
using MetroCalendar.Controls;
using Microsoft.Phone.Controls;

namespace MetroCalendar
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region internal data
        private bool _speaking;
        #endregion

        // Constructor
        #region constructor
        public MainPage()
        {
            InitializeComponent();

            CalendarMonthView.MonthViewRowNumberUpdated += OnMonthViewRowNumberUpdated;
            StopSpeakStoryboard.Completed += OnStopSpeakStoryboardCompleted;
        }
        #endregion

        #region internal methods
        private void MainPage_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void OnMonthViewRowNumberUpdated()
        {
            LayoutSignBoard();
        }

        private void LayoutSignBoard()
        {
            const double RowHeight = 68.0;
            const int MaxVisibleRow = 6;
            int decVisibleRow = MaxVisibleRow - CalendarMonthView.VisibleRow;

            SignBoard.Margin = new Thickness(0, -(RowHeight * decVisibleRow), 0, 0);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutSignBoard();
        }

        private void OnMemoClick(object sender, EventArgs e)
        {
            if (_speaking)
            {
                _speaking = false;
                StopSpeakStoryboard.Begin();
            }
            else
            {
                _speaking = true;
                BeginSpeakStoryboard.Begin();
                SpeakerMemoPanel.Start();
            }
        }

        private void OnAlarmClick(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(
                new Uri("/MetroCalendar;component/AlarmPage.xaml", UriKind.Relative));
        }

        private void OnAnniversaryClick(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(
                new Uri("/MetroCalendar;component/AnniversaryPage.xaml", UriKind.Relative));
        }

        private void OnAboutClick(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(
                new Uri("/MetroCalendar;component/AboutPage.xaml", UriKind.Relative));
        }

        private void OnStopSpeakStoryboardCompleted(object sender, EventArgs e)
        {
            SpeakerMemoPanel.Stop();
        }
        #endregion
    }
}
